/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import java.sql.*;
import java.util.*;
import DTO.clsNhanVien;


/**
 *
 * @author XuanHung
 */
public class NhanVienDAO {
    Statement st;
    ResultSet rs;
    Datacontact dct = new Datacontact();
    public ArrayList<clsNhanVien> getData() throws Exception{
       ArrayList <clsNhanVien> dsnv = new ArrayList<>();
       try{
           st = dct.getM_conn().createStatement();
           String strSQL = "select * from NhanVien";
           clsNhanVien nhanvien;
           rs = st.executeQuery(strSQL);
           while(rs.next()){
               nhanvien = new clsNhanVien(rs.getInt("MaNhanVien"),rs.getNString("HoTen"),rs.getInt("PhongBan"),rs.getNString("ChucVu"),rs.getDouble("LuongCanBan"),rs.getNString("NgaySinh"));
               dsnv.add(nhanvien);
           }
       }
       catch (Exception ex){
           throw new Exception(ex.toString());
       }
        return dsnv;
    }
    public boolean xoaNhanVien(clsNhanVien nv) throws Exception{
        boolean bEqual = false;
        String strSQL = "DELETE FROM nhanvien WHERE MaNhanVien = '"+nv.getM_iMaNhanVien()+"'";
        try{
            st = dct.getM_conn().createStatement();
            st.executeQuery(strSQL);
            bEqual = true;
        }
        catch(Exception ex){
            throw new Exception(ex.toString());
        }
        return bEqual;                
    }
    public boolean luuNhanVien(clsNhanVien nv, boolean bUpdate) throws SQLException{
        boolean bEqual = false;
        String strSQL;
        strSQL = "";
        PreparedStatement ps = null;
        int iMaNhanVien = nv.getM_iMaNhanVien();
        String strHoTen = nv.getM_strHoTen();
        int iPhongBan = nv.getM_iPhongBan();
        String strChucVu = nv.getM_strChucVu();
        double dbLuongCanBan = nv.getM_dbLuongCanBan();
        String strNgaySinh = nv.getM_strNgaySinh();
        if(bUpdate == false){
            strSQL = "INSERT INTO nhanvien(MaNhanVien, HoTen, PhongBan, ChucVu, LuongCanBan, NgaySinh) VALUES (?,?,?,?,?,?)";
            ps = dct.getM_conn().prepareStatement(strSQL);
            ps.setInt(1, iMaNhanVien);
            ps.setString(2, strHoTen);
            ps.setInt(3, iPhongBan);
            ps.setString(4, strChucVu);
            ps.setDouble(5, dbLuongCanBan);
            ps.setString(6, strNgaySinh);
            
        }
        else{
            strSQL = "UPDATE nhanvien SET HoTen = ?,PhongBan = ?,ChucVu = ?,LuongCanBan = ?,NgaySinh = ? WHERE MaNhanVien = ?";
            ps = dct.getM_conn().prepareCall(strSQL);
            ps.setInt(6, iMaNhanVien);
            ps.setString(1, strHoTen);
            ps.setInt(2, iPhongBan);
            ps.setString(3, strChucVu);
            ps.setDouble(4, dbLuongCanBan);
            ps.setString(5, strNgaySinh);
        }
        if(ps.executeUpdate()== 1){
            bEqual = true;
        }
        return bEqual;
    }
}
